package com.terrace.web.tagext.tools;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.terrace.core.constant.ConfigConstant;

/**
 * 资源标签--前台页面导入的静态资源
 * 
 * @author jiangyg
 *
 */
public class ResourceTag extends TagSupport {
	
	private static final long serialVersionUID = 6420582345499974609L;

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(ResourceTag.class);
	
	/**
	 * 加载资源类型
	 */
	protected String types = "default";

	@Override
	public int doStartTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		JspWriter out = this.pageContext.getOut();
		try {
			StringBuffer sb = new StringBuffer();
			String[] ts = types.split(",");
			if (ArrayUtils.contains(ts, "easyui") || ArrayUtils.contains(ts, "bootstrap") || ArrayUtils.contains(ts, "jquery")) {
				sb.append("<script type=\"text/javascript\" src=\"plug-in/jquery/jquery-1.11.3.min.js\"></script>");
				sb.append("<script type=\"text/javascript\" src=\"plug-in/tools/common-utils.js\"></script>");
				sb.append("<script type=\"text/javascript\" src=\"plug-in/tools/common-lang-zh_CN.js\"></script>");
			}
			if (ArrayUtils.contains(ts, "login")) {
				sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"plug-in/login/css/login.css\">");
				sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"plug-in/login/css/animate-custom.css\">");
				sb.append("<script type=\"text/javascript\" src=\"plug-in/login/login.js\"></script>");
			}
			if (ArrayUtils.contains(ts, "lhgdialog")) {
				sb.append("<script type=\"text/javascript\" src=\"plug-in/lhgdialog/lhgdialog.min.js\"></script>");
			}
			if (ArrayUtils.contains(ts, "easyui")) {
				sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"plug-in/easyui/themes/" + easyuiSkin() + "/easyui.css\">");
				sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"plug-in/easyui/themes/icon.css\">");
				sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"plug-in/easyui/themes/easyui.common.css\">");
				sb.append("<script type=\"text/javascript\" src=\"plug-in/easyui/jquery.easyui.min.js\"></script>");
				sb.append("<script type=\"text/javascript\" src=\"plug-in/easyui/easyui.extend.js\"></script>");
				sb.append("<script type=\"text/javascript\" src=\"plug-in/easyui/locale/easyui-lang-zh_CN.js\"></script>");
			}
			if (ArrayUtils.contains(ts, "bootstrap")) {
				sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"plug-in/bootstrap/css/bootstrap.min.css\">");
				sb.append("<script type=\"text/javascript\" src=\"plug-in/bootstrap/js/bootstrap.min.js\"></script>");
			}
			out.print(sb.toString());
		} catch (Exception e) {
			logger.error("生成资源标签出错：", e);
		}
		return EVAL_PAGE;
	}
	
	/**
	 * 获取easyui皮肤
	 * @return
	 */
	private String easyuiSkin() {
		String skinList = ConfigConstant.value("easyui.skin.list");
		if (StringUtils.isNotBlank(skinList)) {
			String[] skins = skinList.split(",");
			HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
			Cookie[] cookies = request.getCookies();
			for (Cookie cookie : cookies) {
				if (StringUtils.equals(cookie.getName(), "easyui-skin")) {
					if (ArrayUtils.contains(skins, cookie.getValue())) {
						return cookie.getValue();
					}
				}
			}
		}
		return "default";
	}
	
	public String getTypes() {
		return types;
	}

	public void setTypes(String types) {
		this.types = types;
	}

}
